package com.appkubes.mall.admin.application.system.ad.controller;


import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.system.ad.entity.vo.AdKeysVO;
import com.appkubes.mall.admin.application.system.ad.model.AdEditResult;
import com.appkubes.mall.admin.application.system.ad.service.impl.AdServiceImpl;
import com.appkubes.mall.admin.application.system.entity.vo.AdVO;
import com.appkubes.mall.admin.config.camel.RequestModel;
import com.appkubes.mall.core.controller.BaseMallAdController;
import com.appkubes.mall.core.entity.MallAd;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("广告模块")
@RestController
@RequestMapping("/sass_admin/Sad")
public class AdController extends BaseMallAdController {


    @Autowired
    AdServiceImpl adServiceImpl;

    @ApiOperation(value = "广告列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "") String key,
                            @RequestParam(defaultValue = "0") Integer type,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @RequestParam(defaultValue = "ad_key") String orderfield,
                            @RequestParam(defaultValue = "desc") String ordertype) {

        IPage<MallAd> pageInfo = adServiceImpl.querySelective(key, type, page, limit);
        return Response.success(pageInfo);
    }

    @ApiOperation(value = "添加广告")
    @PostMapping("/save")
    public Object save(@RequestModel AdVO adVO) {
        MallAd mallAd = new MallAd();
        BeanUtils.copyProperties(adVO, mallAd);

        Boolean flag = this.saveOrUpdate(mallAd);
        if (flag) {
            return Response.success(flag);
        } else {
            return Response.error("保存失败");
        }
    }

    @ApiOperation(value = "广告编辑")
    @PostMapping("/edit")
    public Object edit(@RequestParam String adKey) {
        MallAd model = adServiceImpl.getDao().selectById(adKey);
        AdEditResult result = new AdEditResult(model);
        return Response.success(result);
    }


    @ApiOperation(value = "删除广告")
    @PostMapping("/delete")
    public Response delete(@ParamValid AdKeysVO adKeysVO) {
        Boolean result = adServiceImpl.removeByIds(adKeysVO.getKeys());
        return Response.success(result);
    }

    @ApiOperation(value = "开启广告")
    @PostMapping("/enable")
    public Response enable(@ParamValid AdKeysVO adKeysVO) {
        return Response.success();

    }

    @ApiOperation(value = "禁用广告")
    @PostMapping("/disable")
    public Response disable(@ParamValid AdKeysVO adKeysVO) {
        return Response.success();

    }

    @ApiOperation("开启")
    @PostMapping("/batchenable")
    public Response batchenable(String ids) {
        return Response.success(adServiceImpl.batchenable(ids.split(",")));
    }

    @ApiOperation("关闭")
    @PostMapping("/batchunenable")
    public Response batchunenable(String ids) {
        return Response.success(adServiceImpl.batchunenable(ids.split(",")));
    }
}
